#include "instrumentinfo.h"
#include "ui_instrumentinfo.h"

InstrumentInfo::InstrumentInfo(Instrument inst, QWidget *parent) :
    ins (inst),
    QWidget(parent),
    ui(new Ui::InstrumentInfo)
{
    ui->setupUi(this);

    // LHS
    connect(ui->pitch_read_combo, SIGNAL(activated(int)), this, SLOT(onLHSChanged()));
    connect(ui->alt_read_combo, SIGNAL(activated(int)), this, SLOT(onLHSChanged()));
    connect(ui->octave_read_combo, SIGNAL(activated(int)), this, SLOT(onLHSChanged()));
    // RHS
    connect(ui->pitch_real_combo, SIGNAL(activated(int)), this, SLOT(onRHSChanged()));
    connect(ui->alt_real_combo, SIGNAL(activated(int)), this, SLOT(onRHSChanged()));
    connect(ui->octave_real_combo, SIGNAL(activated(int)), this, SLOT(onRHSChanged()));
    // on Clef Changed
    connect(ui->treble_radio, SIGNAL(toggled(bool)), this, SLOT(onClefChanged()));
    connect(ui->tenor_radio, SIGNAL(toggled(bool)), this, SLOT(onClefChanged()));
    connect(ui->bass_radio, SIGNAL(toggled(bool)), this, SLOT(onClefChanged()));

    // Populate combo
    ui->pitch_read_combo->clear();
    ui->pitch_read_combo->addItems(PitchNoteList());
    ui->pitch_read_combo->setCurrentIndex(PitchNote().toInt());

    ui->alt_read_combo->clear();
    ui->alt_read_combo->addItems(AlterNoteList());
    ui->alt_read_combo->setCurrentIndex(AlterNote().toUnsignedInt());

    ui->octave_read_combo->clear();
    ui->octave_read_combo->addItems(OctaveList());
    ui->octave_read_combo->setCurrentIndex(Octave().toUnsignedInt());

    ui->pitch_real_combo->clear();
    ui->pitch_real_combo->addItems(PitchNoteList());
    ui->pitch_real_combo->setCurrentIndex(PitchNote().toInt());

    ui->alt_real_combo->clear();
    ui->alt_real_combo->addItems(AlterNoteList());
    ui->alt_real_combo->setCurrentIndex(AlterNote().toUnsignedInt());

    ui->octave_real_combo->clear();
    ui->octave_real_combo->addItems(OctaveList());
    ui->octave_real_combo->setCurrentIndex(Octave().toUnsignedInt());

    // Generate first instrument
    updateInstrument();

    // Update first
    onLHSChanged();
}

InstrumentInfo::~InstrumentInfo()
{
    delete ui;
}

void InstrumentInfo::updateInstrument()
{
    // Clef
    clf = ins.getClef();
    ui->clef_label->setPixmap(clf.getQPixmap());
    if(clf == Clef(0)) ui->treble_radio->toggle();
    else if(clf == Clef(1)) ui->tenor_radio->toggle();
    else if(clf == Clef(2)) ui->bass_radio->toggle();

    // General details
    QString string = "<b>" + ins.getInstrumentName() + "</b>";
    QString toname = ins.getTonaltyName();
    if (toname != "N/D") string += " in " + toname;
    ui->name_label->setText(string);

    ui->class_label->setText(ins.getClassName());
    ui->type_label->setText(ins.getTypeName());

    // Tonality
    ton_note = ins.getTonalityNote();
    ui->fund_note_label->setText(ton_note.getEquiNoteName());
    ui->fund_oct_label->setText(ton_note.getOctaveName());

    // Transposing
    if( ins.isTransposingInstrument() ) {
        ui->conversion_frame->show();
        trp_note = ins.getTranposeNote();
        ui->ton_marker->show();
        ui->ton_oct_marker->show();
        ui->transpose_pitch_label->show();
        ui->transpose_octave_label->show();
        ui->transpose_preview_label->show();
        ui->transpose_pitch_label->setText(trp_note.getEquiNoteName());
        ui->transpose_octave_label->setText(trp_note.getOctaveName());
        string = tr("This instrument is a transposing instrument.");
    } else {
        ui->conversion_frame->hide();
        ui->ton_marker->hide();
        ui->ton_oct_marker->hide();
        ui->transpose_pitch_label->hide();
        ui->transpose_octave_label->hide();
        ui->transpose_preview_label->hide();
        string = tr("This instrument is not a transposing instrument.");
    }
    ui->istransposing_label->setText(string);

    // Repaint Notes
    onLHSChanged();
}

void InstrumentInfo::repaintPixmaps()
{
    ui->fund_preview_label->setPixmap(ton_note.getQPixmap(clf));
    ui->transpose_preview_label->setPixmap(trp_note.getQPixmap(clf));
    ui->read_note_preview->setPixmap(read_note.getQPixmap(clf));
    ui->real_note_preview->setPixmap(real_note.getQPixmap(clf));
}

void InstrumentInfo::onLHSChanged()
{
    int p, a, o;
    // Read index on left
    p = ui->pitch_read_combo->currentIndex();
    a = ui->alt_read_combo->currentIndex();
    o = ui->octave_read_combo->currentIndex();
    // Generate note and convert ans set preview
    read_note = Note(p,a,o, true, true);
    real_note = ins.getRealNote(read_note);
    // Generate indexes
    p = real_note.toPitchNote().toInt();
    a = real_note.toAlterNote().toUnsignedInt();
    o = real_note.toOctave().toUnsignedInt();
    // Set right value
    ui->pitch_real_combo->setCurrentIndex(p);
    ui->alt_real_combo->setCurrentIndex(a);
    ui->octave_real_combo->setCurrentIndex(o);

    repaintPixmaps();
}

void InstrumentInfo::onRHSChanged()
{
    int p, a, o;
    // Read index on right
    p = ui->pitch_real_combo->currentIndex();
    a = ui->alt_real_combo->currentIndex();
    o = ui->octave_real_combo->currentIndex();
    // Generate note and convert ans set preview
    real_note = Note(p,a,o, true, true);
    read_note = ins.getReadedNote(real_note);
    // Generate indexes
    p = read_note.toPitchNote().toInt();
    a = read_note.toAlterNote().toUnsignedInt();
    o = read_note.toOctave().toUnsignedInt();
    // Set left value
    ui->pitch_read_combo->setCurrentIndex(p);
    ui->alt_read_combo->setCurrentIndex(a);
    ui->octave_read_combo->setCurrentIndex(o);

    repaintPixmaps();
}

void InstrumentInfo::onClefChanged()
{
    if(ui->treble_radio->isChecked()) clf = Clef(0);
    if(ui->tenor_radio->isChecked()) clf = Clef(1);
    if(ui->bass_radio->isChecked()) clf = Clef(2);

    repaintPixmaps();
}
